package com.haoxi.gateway.cmppmsg;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.haoxi.gateway.util.TypeConvert;

public class CMPPSubmitMessage extends CMPPMessage {

    private StringBuffer outBuf;

    public CMPPSubmitMessage(int pk_Total, int pk_Number,
            int registered_Delivery, int msg_Level, String service_Id,
            int fee_UserType, String fee_Terminal_Id, int tp_Pid, int tp_Udhi,
            int msg_Fmt, String msg_Src, String fee_Type, String fee_Code,
            Date valid_Time, Date at_Time, String src_Terminal_Id,
            String[] dest_Terminal_Id, byte[] msg_Content, String reserve)
            throws IllegalArgumentException {
        if (pk_Total < 1 || pk_Total > 255)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:pk_Total less than 1 or larger than 255.");
        if (pk_Number < 1 || pk_Number > 255)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:pk_Number less than 1 or larger than 255.");
        if (registered_Delivery < 0 || registered_Delivery > 2)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:registered_Delivery less than 0 or larger than 2.");
        if (msg_Level < 0 || msg_Level > 255)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:msg_Level less than 0 or larger than 255.");
        if (service_Id.length() > 10)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:service_Id length is larger than 10");
        if (fee_UserType < 0 || fee_UserType > 3)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:fee_UserType less than 0 or larger than 3.");
        if (fee_Terminal_Id.length() > 21)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:fee_Terminal_Id length is larger than 21");
        if (tp_Pid < 0 || tp_Pid > 255)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:tp_Pid less than 0 or larger than 255.");
        if (tp_Udhi < 0 || tp_Udhi > 255)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:tp_Udhi less than 0 or larger than 255.");
        if (msg_Fmt < 0 || msg_Fmt > 255)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:msg_Fmt less than 0 or larger than 255.");
        if (msg_Src.length() > 6)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:msg_Src length is larger than 6");
        if (fee_Type.length() > 2)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:fee_Type length is larger than 2");
        if (fee_Code.length() > 6)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:fee_Code length is larger than 6");
        if (src_Terminal_Id.length() > 21)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:src_Terminal_Id length is larger than 21");
        if (dest_Terminal_Id.length > 100)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:dest_Terminal_Id length is larger than 100");
        for (int i = 0; i < dest_Terminal_Id.length; i++)
            if (dest_Terminal_Id[i].length() > 21)
                throw new IllegalArgumentException(
                        "The parameter of submit message is wrong:dest_Terminal_Id["
                                + i + "]length is larger than 21");

        if (msg_Fmt == 0) {
            if (msg_Content.length > 160)
                throw new IllegalArgumentException(
                        "The parameter of submit message is wrong:msg_Content length is larger than 160");
        } else if (msg_Content.length > 140)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:msg_Content length is larger than 140");
        if (reserve.length() > 8)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:reserve length is larger than 8");
        int len = 138 + 21 * dest_Terminal_Id.length + msg_Content.length;
        buf = new byte[len];
        TypeConvert.int2byte(len, buf, 0);
        TypeConvert.int2byte(CMPPCommon.CMPP_SUBMIT, buf, 4);
        buf[20] = (byte) pk_Total;
        buf[21] = (byte) pk_Number;
        buf[22] = (byte) registered_Delivery;
        buf[23] = (byte) msg_Level;
        System
                .arraycopy(service_Id.getBytes(), 0, buf, 24, service_Id
                        .length());
        buf[34] = (byte) fee_UserType;
        System.arraycopy(fee_Terminal_Id.getBytes(), 0, buf, 35,
                fee_Terminal_Id.length());
        buf[56] = (byte) tp_Pid;
        buf[57] = (byte) tp_Udhi;
        buf[58] = (byte) msg_Fmt;
        System.arraycopy(msg_Src.getBytes(), 0, buf, 59, msg_Src.length());
        System.arraycopy(fee_Type.getBytes(), 0, buf, 65, fee_Type.length());
        System.arraycopy(fee_Code.getBytes(), 0, buf, 67, fee_Code.length());
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyMMddHHmmss");
        if (valid_Time != null) {
            String tmpTime = dateFormat.format(valid_Time).concat("032+");
            System.arraycopy(tmpTime.getBytes(), 0, buf, 73, 16);
        }
        if (at_Time != null) {
            String tmpTime = dateFormat.format(at_Time).concat("032+");
            System.arraycopy(tmpTime.getBytes(), 0, buf, 90, 16);
        }
        System.arraycopy(src_Terminal_Id.getBytes(), 0, buf, 107,
                src_Terminal_Id.length());
        buf[128] = (byte) dest_Terminal_Id.length;
        int i = 0;
        for (i = 0; i < dest_Terminal_Id.length; i++)
            System.arraycopy(dest_Terminal_Id[i].getBytes(), 0, buf,
                    129 + i * 21, dest_Terminal_Id[i].length());

        int loc = 129 + i * 21;
        buf[loc] = (byte) msg_Content.length;
        System.arraycopy(msg_Content, 0, buf, loc + 1, msg_Content.length);
        loc = loc + 1 + msg_Content.length;
        System.arraycopy(reserve.getBytes(), 0, buf, loc, reserve.length());
        outBuf = new StringBuffer(",msg_id=00000000");
        outBuf.append(",pk_Total=").append(pk_Total);
        outBuf.append(",pk_Number=").append(pk_Number);
        outBuf.append(",registered_Delivery=").append(registered_Delivery);
        outBuf.append(",msg_Level=").append(msg_Level);
        outBuf.append(",service_Id=").append(service_Id);
        outBuf.append(",fee_UserType=").append(fee_UserType);
        outBuf.append(",fee_Terminal_Id=").append(fee_Terminal_Id);
        outBuf.append(",tp_Pid=").append(tp_Pid);
        outBuf.append(",tp_Udhi=").append(tp_Udhi);
        outBuf.append(",msg_Fmt=").append(msg_Fmt);
        outBuf.append(",msg_Src=").append(msg_Src);
        outBuf.append(",fee_Type=").append(fee_Type);
        outBuf.append(",fee_Code=").append(fee_Code);
        if (valid_Time != null) {
            outBuf.append(",valid_Time=").append(dateFormat.format(valid_Time));
        } else {
            outBuf.append(",valid_Time=null");
        }
        if (at_Time != null) {
            outBuf.append(",at_Time=").append(dateFormat.format(at_Time));
        } else {
            outBuf.append(",at_Time=null");
        }
        outBuf.append(",src_Terminal_Id=").append(src_Terminal_Id);
        outBuf.append(",destusr_Tl=").append(dest_Terminal_Id.length);
        for (int t = 0; t < dest_Terminal_Id.length; t++) {
            outBuf.append(",dest_Terminal_Id[").append(t).append("]=").append(
                    dest_Terminal_Id[t]);
        }
        outBuf.append(",msg_Length=").append(msg_Content.length);
        outBuf.append(",msg_Content=").append(new String(msg_Content));
        outBuf.append(",msg_content.length=").append(new String(msg_Content).length());
        outBuf.append(",reserve=").append(reserve);

    }

    public String toString() {
        StringBuffer tmpBuf = new StringBuffer("CMPP_SubmitMessage: ");
        tmpBuf.append("Sequence_Id=").append(getSequenceId());
        tmpBuf.append(outBuf.toString());
        return tmpBuf.toString();
    }

    public int getCommandId() {
        return CMPPCommon.CMPP_SUBMIT;
    }
}
